<?php
declare (strict_types=1);

namespace app\v1\controller;

use app\data\logic\PageLogic;
use app\v1\BaseController;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\App;
use think\facade\Log;

class FeedBack extends BaseController
{
    protected PageLogic $pageLogic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->pageLogic = new PageLogic();
    }

    /**
     * 反馈列表
     */
    public function index()
    {
        $result = $this->pageLogic->getFeedBackList($this->request->get());
        if ($result) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'), $result['list'], $result['count']);
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }

    public function create()
    {
        $params = $this->request->get();
        $params['page'] = 1;
        $params['limit'] = $this->pageLogic->getFeedBackCount($params);
        $data = $this->pageLogic->getFeedBackList($params)['list'];

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $fields = [
            "msg_id" => "消息id",
            "user_name" => "用户名",
            "user_tel" => "电话",
            "user_email" => "邮箱",
            "msg_content" => "留言",
            "msg_time" => "留言时间",
        ];

        $code = 65;
        $fieldsKeys = array_keys($fields);
        // 设置表头
        for ($i = 0; $i < count($fields); $i++) {
            $sheet->setCellValue(chr($code + $i) . '1', $fields[$fieldsKeys[$i]]);
        }

        $row = "2";
        foreach ($data as $item) {
            for ($i = 0; $i < count($fields); $i++) {
                $sheet->setCellValue(chr($code + $i) . $row, $item[$fieldsKeys[$i]]);
            }
            $row++;
        }

        // 创建 Excel 写入器
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');

        // 输出到浏览器并提示下载
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="export.xlsx"');
        header('Cache-Control: max-age=0');

        $writer->save('php://output');
    }

    /**
     * 软删除反馈信息
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id)
    {
        $result = $this->pageLogic->deleteFeedback($id);
        if ($result['state']) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }


    /**
     * 更新反馈信息
     * @param $id
     * @return \think\response\Json
     */
    public function update($id)
    {
        $params = $this->request->put();
        $checkData['id'] = $id;
        $result = $this->pageLogic->updateFeedback($id, $params, $checkData);
        if ($result['state']) {
            return $this->respMsg(\config('code.SUCCESS'), \lang('SUCCESS'));
        } else {
            return $this->respMsg(\config('code.FAILURE'), \lang('FAILURE'));
        }
    }
}
